package com.ceadeal.admin.service;

import com.ceadeal.admin.util.BaseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 功能描述: Base Service
 *
 * 作者: Yzw
 * 日期: 2017-03-08 09:44:14
 */
@Service
@Transactional
public abstract class BaseService<T> {

    @Autowired
    @SuppressWarnings("SpringJavaAutowiringInspection")
    protected BaseMapper<T> mapper;

    public BaseMapper<T> getMapper() {
        return mapper;
    }

    public int save(T entity)
    {
        return mapper.insertSelective(entity);
    }

    public int save(List<T> list) {
        return mapper.insertList(list);
    }

    public int delete(Object key) {
        return mapper.deleteByPrimaryKey(key);
    }

    public int deleteByExample(Object example) {
        return mapper.deleteByExample(example);
    }

    public int update(T entity) {
        return mapper.updateByPrimaryKeySelective(entity);
    }

    public int updateByExample(T entity, Object example) {
        return mapper.updateByExampleSelective(entity, example);
    }

    @Transactional(readOnly = true)
    public T find(Object key) {
        return mapper.selectByPrimaryKey(key);
    }

    @Transactional(readOnly = true)
    public List<T> findAll() {
        return mapper.selectAll();
    }

    @Transactional(readOnly = true)
    public List<T> findByExample(Object example) {
        return mapper.selectByExample(example);
    }
}
